import Vue from 'vue' // 引入vue的基础库
import VueRouter from 'vue-router' // 引入vue-router
import Footer from '../components/Footer'
// import store from '../store'

Vue.use(VueRouter) // Vue 使用VueRouter

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'home',
    components: {
      default: () => import(/* webpackChunkName: "group-base" */'../views/home/index.vue'),
      footer: Footer
    },
    meta: { // 路由的原信息，使用meta选项，内部的值随便指定
      // keepAlive: true
    }
  },
  {
    path: '/classify',
    name: 'classify',
    components: {
      default: () => import(/* webpackChunkName: "group-base" */'../views/classify/index.vue'),
      footer: Footer
    },
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/classifyList',
    name: 'classifyList',
    component: () => import(/* webpackChunkName: "group-category" */'../views/classify/list.vue')
  },
  {
    path: '/user',
    name: 'user',
    components: {
      default: () => import(/* webpackChunkName: "group-base" */'../views/user/index.vue'),
      footer: Footer
    }
  },
  {
    path: '/cart',
    name: 'cart',
    components: {
      default: () => import(/* webpackChunkName: "group-base" */'../views/cart/index.vue'),
      footer: Footer
    }
    // beforeEnter (to, from, next) {
    //   if (!store.state.user.loginState) {
    //     next('/login')
    //   } else {
    //     next()
    //   }
    // }
  },
  {
    path: '/detail/:proid',
    name: 'detail',
    component: () => import(/* webpackChunkName: "group-base" */'../views/details/index.vue')
  },
  {
    path: '/register',
    name: 'register',
    component: () => import(/* webpackChunkName: "group-user" */'../views/register/index.vue')
  },
  {
    path: '/sms',
    name: 'sms',
    component: () => import(/* webpackChunkName: "group-user" */'../views/register/sms.vue')
  },
  {
    path: '/setpwd',
    name: 'setpwd',
    component: () => import(/* webpackChunkName: "group-user" */'../views/register/setpwd.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "group-user" */'../views/login/index.vue')
  },
  {
    path: '/order/:time',
    name: 'order',
    component: () => import(/* webpackChunkName: "group-order" */'../views/order/index.vue')
  },
  {
    path: '/orderaddresslist/:time',
    name: 'orderlist',
    component: () => import(/* webpackChunkName: "group-order" */'../views/order/list.vue')
  },
  {
    path: '/orderaddressadd/:time',
    name: 'orderadd',
    component: () => import(/* webpackChunkName: "group-order" */'../views/order/add.vue')
  },
  {
    path: '/search',
    name: 'search',
    component: () => import(/* webpackChunkName: "group-search" */'../views/search/index.vue')
  },
  {
    path: '/searchlist',
    name: 'searchlist',
    component: () => import(/* webpackChunkName: "group-search" */'../views/search/list.vue')
  },
  {
    path: '/pay',
    name: 'pay',
    component: () => import(/* webpackChunkName: "group-order" */'../views/pay/index.vue')
  }
]

// 生成路由的实例
const router = new VueRouter({
  mode: 'hash', // 展示的样式    /#/ mode还可以是history  /
  base: process.env.BASE_URL,
  routes // 路由的规则
  // scrollBehavior (to, from, savedPosition) {
  //   console.log('savedPosition', savedPosition)
  //   if (savedPosition) {
  //     return savedPosition
  //   } else {
  //     return { x: 0, y: 0 }
  //   }
  // }
})

// 全局的导航守卫 适用于绝对大多数的路由都要满足的条件，只有部分需要验证的可以不使用全局导航守卫 此项目不推荐使用
// router.beforeEach((to, from, next) => {
//   console.log('to', to)
//   console.log('from', from)
//   if (to.name === 'cart') {
//     if (store.state.user.loginState) {
//       next()
//     } else {
//       next('/login')
//     }
//   } else {
//     next()
//   }
// })

export default router
